<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="收货人" prop="contactName">
        <el-input
          v-model="queryParams.contactName"
          placeholder="请输入收货人"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="运单状态" prop="status">
        <el-select
          v-model="queryParams.waybill_status"
          placeholder="运单状态"
          clearable
          size="small"
          style="width: 140px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单时间" prop="orderDate">
        <el-date-picker
          v-model="queryParams.orderDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="openImport"
        >物流导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-male"
          size="mini"
          :disabled="multiple"
          @click="pushAll()"
        >批量推送物流</el-button>
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          plain-->
        <!--          icon="el-icon-edit"-->
        <!--          size="mini"-->
        <!--          @click="modifyInfoAll"-->
        <!--        >批量推送物流</el-button>-->
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" :selectable="checkSelectable" />
      <!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="订单日期" align="center" prop="orderDate" />
      <el-table-column label="收货人" align="center" prop="contactName" />
      <el-table-column label="收货地址" width="300" align="center" :show-overflow-tooltip="true">
        <template v-slot="scope">
          {{ scope.row.contactAddr + " " + scope.row.detailAddr }}
        </template>
      </el-table-column>
      <el-table-column label="运单状态" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.waybill_status === 0">未推送</span>
          <span v-if="scope.row.waybill_status === 1">推送成功</span>
          <span v-if="scope.row.waybill_status === 2">新增申报成功</span>
          <span v-if="scope.row.waybill_status === 120">海关入库</span>
          <span v-if="scope.row.waybill_status === 20">推送失败</span>
        </template>
      </el-table-column>

      <!--      <el-table-column label="回执信息" align="center" prop="error_msg" :show-overflow-tooltip="true" />-->
      <el-table-column label="运单号" align="center" prop="bill_no" />
      <!--      <el-table-column label="推送失败原因" align="center" prop="error_msg" />-->
      <el-table-column label="操作" align="center" min-width="100px" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="primary"
            @click="see(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="success"
            :disabled="scope.row.waybill_status !== 1 ? false : true"
            @click="pushAll(scope.row)"
          >推送运单</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
    <!-- 查看详情 -->
    <LogisticsDetails :show.sync="show" :form="form" />
    <!-- 物流导入 -->
    <el-dialog v-loading="export_loading" title="物流导入" :visible.sync="export_open" width="800px" append-to-body>
      <el-form ref="form" label-width="80px">
        <el-form-item label="模板下载">
          <el-button type="primary" size="mini" @click="downTemplate">点击下载</el-button>
        </el-form-item>
        <el-form-item label="上传文件">
          <el-upload
            ref="upload"
            class="upload-demo"
            :headers="importHeaders"
            :action="url.import"
            :on-success="handleSuccess"
            :on-change="handleImport"
            :file-list="fileList"
            :multiple="false"
            :auto-upload="false"
            :limit="1"
            accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
          >
            <el-button size="mini" type="success">选择文件</el-button>
            <div slot="tip" class="el-upload__tip">只能上传csv/xlsx/xls文件</div>
          </el-upload>
        </el-form-item>
        <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
          <el-col :span="24">
            <el-button type="primary" size="medium" @click="submitUpload">导 入</el-button>
            <el-button size="medium" @click="export_open = false">关 闭</el-button>

          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { sinotransList, sinotransPush, downTemplate } from '@/api/logistics'
import { orderInfo } from '@/api/order'
import { mapGetters } from 'vuex'
export default {
  name: 'Index',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      export_loading: false,
      export_open: false,
      fileList: [],
      url: {
        down: '',
        import: ''
      },
      // 非多个禁用
      multiple: true,
      statusOptions: [
        { label: '未推送', value: 0 },
        { label: '推送成功', value: 1 },
        { label: '新增申报成功', value: 2 },
        { label: '海关入库', value: 120 },
        { label: '推送失败', value: 20 }
      ],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        orderId: undefined,
        contactName: undefined,
        waybill_status: undefined,
        orderDate: []
      },
      ids: [],
      show: false,
      form: {},
      importAction: '',
      importHeaders: { authorization: 'Bearer ' + this.token },
      importList: [],
      extraImport: {},
      importLoading: false
    }
  },
  computed: {
    ...mapGetters([
      'token'
    ])
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true
      sinotransList(this.queryParams).then(response => {
        if (response.code === 10000) {
          this.list = response.data
          this.total = response.count
          this.loading = false
        }
      })
    },
    /** 查询订单详情 */
    see(row) {
      orderInfo({ orderId: row.orderId, type: 1 }).then(response => {
        if (response.code === 10000) {
          this.form = response.data
          this.show = true
        }
      })
    },
    /** 推送物流单 */
    pushAll(row) {
      const that = this
      const postIds = row ? row.orderId : this.ids.toString()
      this.$confirm('是否推送' + postIds + '等物流信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        sinotransPush({ orderId: postIds }).then(response => {
          // console.log(response.message)
          if (response.code === 10000) {
            that.$message.success(response.message)
          }
          that.getList()
        })
      }).catch(() => {})
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        orderId: undefined,
        contactName: undefined,
        waybill_status: undefined,
        orderDate: []
      }
      this.handleQuery()
    },
    /** 可以选中的项 */
    checkSelectable(row) {
      return row.waybill_status === 0 || row.waybill_status === 2
    },
    // 导入提交
    submitUpload() {
      this.$refs.upload.submit()
    },
    /** 选中的项 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.multiple = !selection.length
    },
    // 打开导入弹窗
    handleImport() {
      this.importHeaders.authorization = 'Bearer ' + this.token
      this.url.import = process.env.VUE_APP_BASE_API + '/logistics/sinotransImportLogistics'
    },
    // 打开导入弹窗
    openImport() {
      this.export_open = true
      if (this.$refs.upload) {
        this.$refs.upload.clearFiles()
      }
    },
    // 导入成功提示
    handleSuccess(response, file, fileList) {
      if (response.code === 10000) {
        this.$message.success(response.message)
        this.export_open = false
      } else {
        this.$message.error(response.message)
      }
    },
    // 下载模板
    downTemplate() {
      const that = this
      downTemplate({ name: 'logistics' }).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          window.open(response.data.url)
        } else {
          that.$message.error(response.message)
        }
      })
    }
  }
}
</script>
